﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Green.Utility
{
    public static class AttributeExtensions
    {
        #region Attribute
        #region Type
        /// <summary>
        /// 获取唯一Attribute
        /// </summary>
        /// <param name="type"></param>
        /// <param name="attribute"></param>
        /// <param name="inherit">是否继承</param>     
        public static object GetAttribte(this Type type, Type attribute, bool inherit)
        {
            object[] attrs;
            attrs = GetAttribtes(type, attribute, false);
            if (attrs == null || attrs.Length == 0)
                return null;
            return attrs[0];
        }
        /// <summary>
        /// 获取唯一Attribute，不包含继承
        /// </summary>
        /// <param name="type"></param>
        /// <param name="attribute"></param>              
        public static object GetAttribte(this Type type, Type attribute)
        {
            return GetAttribte(type, attribute, false);
        }

        /// <summary>
        /// 取所有Attribute
        /// </summary>
        /// <param name="type"></param>
        /// <param name="attribute"></param>
        /// <param name="inherit">是否继承</param>
        public static object[] GetAttribtes(this Type type, Type attribute, bool inherit)
        {
            var attrs = type.GetCustomAttributes(attribute, inherit);
            return attrs;
        }
        /// <summary>
        /// 取所有Attribute，不包含继承
        /// </summary>
        /// <param name="type"></param>
        /// <param name="attribute"></param>               
        public static object[] GetAttribtes(this Type type, Type attribute)
        {
            return GetAttribtes(type, attribute, false);
        }

        public static T GetAttribte<T>(this Type type, bool inherit) where T : class
        {
            object attr = GetAttribte(type, typeof(T), inherit);
            return (T)attr;
        }
        public static T GetAttribte<T>(this Type type) where T : class
        {
            return GetAttribte<T>(type, false);
        }

        public static T[] GetAttribtes<T>(this Type type, bool inherit) where T : class
        {
            return GetAttribtes(type, typeof(T), false) as T[];
        }
        public static T[] GetAttribtes<T>(this Type type) where T : class
        {
            return GetAttribtes<T>(type, false);
        }
        #endregion

        #region MemberInfo

        /// <summary>
        /// 获取唯一Attribute
        /// </summary>
        /// <param name="member"></param>
        /// <param name="attribute"></param>
        /// <param name="inherit">是否继承</param>
        public static object GetAttribte(this System.Reflection.MemberInfo type, Type attribute, bool inherit)
        {
            object[] attrs = GetAttribtes(type, attribute, false);
            if (attrs == null || attrs.Length == 0)
                return null;
            return attrs[0];
        }
        /// <summary>
        /// 获取唯一Attribute，不包含继承
        /// </summary>
        /// <param name="member"></param>
        /// <param name="attribute"></param>
        public static object GetAttribte(this System.Reflection.MemberInfo type, Type attribute)
        {
            return GetAttribte(type, attribute, false);
        }

        /// <summary>
        /// 取所有Attribute
        /// </summary>
        /// <param name="member"></param>
        /// <param name="attribute"></param>
        /// <param name="inherit">是否继承</param>
        public static object[] GetAttribtes(this System.Reflection.MemberInfo member, Type attribute, bool inherit)
        {
            return member.GetCustomAttributes(attribute, inherit);
        }
        /// <summary>
        /// 取所有Attribute，不包含继承
        /// </summary>
        /// <param name="member"></param>
        /// <param name="attribute"></param>
        public static object[] GetAttribtes(this System.Reflection.MemberInfo member, Type attribute)
        {
            return GetAttribtes(member, attribute, false);
        }

        public static T GetAttribte<T>(this System.Reflection.MemberInfo member, bool inherit) where T : class
        {
            object attr = GetAttribte(member, typeof(T), inherit);
            return (T)attr;
        }
        public static T GetAttribte<T>(this System.Reflection.MemberInfo member) where T : class
        {
            return GetAttribte<T>(member, false);
        }

        public static  T[] GetAttribtes<T>(this System.Reflection.MemberInfo member, bool inherit) where T : class
        {
            return GetAttribtes(member, typeof(T), false) as T[];
        }
        public static T[] GetAttribtes<T>(this System.Reflection.MemberInfo member) where T : class
        {
            return GetAttribtes<T>(member, false);
        }
        #endregion
        #endregion

    }
}
